<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar
      title="编辑资料"
      left-arrow
      @click-left="$router.back()"
      fixed
    />

    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center>
        <template #default>
          <van-image
            round
            class="avatar"
            :src="userProfileObj.photo"
            @click="$refs.iptFile.click()"
          />
          <input
            type="file"
            ref="iptFile"
            v-show="false"
            @change="onFileChange"
          />
        </template>
      </van-cell>
      <van-cell
        title="名称"
        is-link
        :value="userProfileObj.name"
        @click="show = true"
      />

      <van-dialog
        v-model="show"
        title="标题"
        show-cancel-button
        :before-close="onNameDialogBeforeClose"
      >
        <van-field
          v-model.trim="username"
          input-align="center"
          maxlength="7"
          placeholder="请输入名称"
          v-fofo
        />
      </van-dialog>
      <van-cell title="生日" is-link :value="userProfileObj.birthday" @click="isShowBirth=true" />
      <van-popup
        v-model="isShowBirth"
        position="bottom"
        style="height: 50%"
        round
      >
        <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @confirm ='confirmFn'
        @cancel ='isShowBirth=false'
        />
      </van-popup>
    </van-cell-group>
  </div>
</template>

<script>
import { userProfileAPI, updatePhotoAPI, updateUserProfileAPI } from '@/api'
import { Notify } from 'vant'
export default {
  name: 'UserEdit',
  data () {
    return {
      userProfileObj: {},
      show: false,
      isShowBirth: false,
      username: '',
      minDate: new Date(1900, 0, 1),
      maxDate: new Date(),
      currentDate: new Date()
    }
  },
  async created () {
    const res = await userProfileAPI()
    console.log(res)
    this.userProfileObj = res.data.data
    this.username = res.data.data.name
    this.currentDate = new Date(res.data.data.birthday)
  },
  methods: {
    async onFileChange (e) {
      if (e.target.files.length === 0) return
      const formObj = new FormData()
      formObj.append('photo', e.target.files[0])
      const res = await updatePhotoAPI({ formObj })
      console.log(res)
      this.userProfileObj.photo = res.data.data.photo
    },
    async onNameDialogBeforeClose (action, done) {
      // action的值: confirm或cancel(点击按钮区分)
      if (action === 'confirm') {
        // 确定
        // unicode编码 \u4092
        // url编码 %E2%C3%D1
        if (/^[A-Za-z0-9\u4e00-\u9fa5]{1,7}$/.test(this.username)) {
          this.userProfileObj.name = this.username
          // 通过校验
          // 调用接口
          // 更新页面显示的名字

          // 关闭弹窗
          console.log(this.username)
          await updateUserProfileAPI({ name: this.username })
          done()
        } else {
          // 提示用户
          Notify({ type: 'warning', message: '用户名中英文和数字1-7位' })
          // 阻止弹窗关闭
          done(false)
        }
      } else if (action === 'cancel') {
        // 取消
        done()
      }
    },
    confirmFn () {
      this.isShowBirth = false
    }
  }
}
</script>

<style lang="less" scoped>
.user-edit-container {
  padding-top: 46px;
  .avatar {
    width: 50px;
    height: 50px;
  }
}
</style>
